// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Status.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework
{
    #region Usings

    using System;

    #endregion

    /// <summary>
    /// The security check status.
    /// </summary>
    [Serializable]
    public class Status
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Status"/> class.
        /// </summary>
        /// <param name="identity">
        /// The Identity.
        /// </param>
        /// <param name="success">
        /// The success.
        /// </param>
        /// <param name="detailedMessage">
        /// The detailedMessage.
        /// </param>
        protected Status(Identity identity, bool success, string detailedMessage)
        {
            this.Sucess = success;
            this.DetailedMessage = detailedMessage;
            this.Identity = identity;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Status"/> class.
        /// </summary>
        /// <param name="identity">
        /// The Identity.
        /// </param>
        /// <param name="success">
        /// The success.
        /// </param>
        /// <param name="detailedMessage">
        /// The detailedMessage.
        /// </param>
        /// <param name="nestedStatus">
        /// The nested Status.
        /// </param>
        protected Status(Identity identity, bool success, string detailedMessage, Status nestedStatus)
        {
            this.Sucess = success;
            this.DetailedMessage = detailedMessage;
            this.NestedStatus = nestedStatus;
            this.Identity = identity;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets DetailedMessage.
        /// </summary>
        public string DetailedMessage { get; private set; }

        /// <summary>
        /// Gets IdentityString of the .
        /// </summary>
        public Identity Identity { get; private set; }

        /// <summary>
        /// Gets NestedStatus.
        /// </summary>
        public Status NestedStatus { get; private set; }

        /// <summary>
        /// Gets a value indicating whether Sucess.
        /// </summary>
        public bool Sucess { get; private set; }

        #endregion

        #region Operators

        /// <summary>
        /// Operator ton convert success to bool
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static implicit operator bool(Status s)
        {
            return s.Sucess;
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Create a status respresenting some unsuccessful action
        /// </summary>
        /// <param name="identity">
        /// The Identity.
        /// </param>
        /// <param name="detailedMessage">
        /// The detailed Message.
        /// </param>
        /// <returns>
        /// A status populated accordingly to represent a failure
        /// </returns>
        public static Status Failure(Identity identity, string detailedMessage)
        {
            return new Status(identity, false, detailedMessage);
        }

        /// <summary>
        /// Create a status respresenting some unsuccessful action
        /// </summary>
        /// <typeparam name="T">
        /// </typeparam>
        /// <param name="identity">
        /// The Identity.
        /// </param>
        /// <param name="detailedMessage">
        /// The detailed Message.
        /// </param>
        /// <returns>
        /// A status populated accordingly to represent a failure
        /// </returns>
        public static Status<T> Failure<T>(Identity identity, string detailedMessage)
        {
            return new Status<T>(identity, false, detailedMessage);
        }

        /// <summary>
        /// Create a status respresenting some unsuccessful action
        /// </summary>
        /// <param name="identity">
        /// The Identity.
        /// </param>
        /// <param name="detailedMessage">
        /// The detailed Message.
        /// </param>
        /// <param name="nestedStatus">
        /// The nested Status.
        /// </param>
        /// <returns>
        /// A status populated accordingly to represent a failure
        /// </returns>
        public static Status Failure(Identity identity, string detailedMessage, Status nestedStatus)
        {
            return new Status(identity, false, detailedMessage, nestedStatus);
        }

        /// <summary>
        /// Create a status respresenting some unsuccessful action
        /// </summary>
        /// <typeparam name="T">
        /// </typeparam>
        /// <param name="identity">
        /// The Identity.
        /// </param>
        /// <param name="detailedMessage">
        /// The detailed Message.
        /// </param>
        /// <param name="nestedStatus">
        /// The nested Status.
        /// </param>
        /// <returns>
        /// A status populated accordingly to represent a failure
        /// </returns>
        public static Status<T> Failure<T>(Identity identity, string detailedMessage, Status nestedStatus)
        {
            return new Status<T>(identity, false, detailedMessage, nestedStatus);
        }

        /// <summary>
        /// Create a status respresenting some successful action
        /// </summary>
        /// <param name="identity">
        /// The Identity.
        /// </param>
        /// <returns>
        /// A status populated accordingly to represent a success
        /// </returns>
        public static Status Success(Identity identity)
        {
            return new Status(identity, true, string.Empty);
        }

        /// <summary>
        /// Create a status respresenting some successful action
        /// </summary>
        /// <typeparam name="T">
        /// </typeparam>
        /// <param name="identity">
        /// The Identity.
        /// </param>
        /// <param name="nestedObject">
        /// The nested Object.
        /// </param>
        /// <returns>
        /// A status populated accordingly to represent a success
        /// </returns>
        public static Status<T> Success<T>(Identity identity, T nestedObject)
        {
            return new Status<T>(identity, true, string.Empty, nestedObject);
        }

        /// <summary>
        /// Create a status respresenting some successful action
        /// </summary>
        /// <param name="identity">
        /// The Identity.
        /// </param>
        /// <param name="detailedMessage">
        /// The detailed Message.
        /// </param>
        /// <returns>
        /// A status populated accordingly to represent a success
        /// </returns>
        public static Status Success(Identity identity, string detailedMessage)
        {
            return new Status(identity, true, detailedMessage);
        }

        /// <summary>
        /// Create a status respresenting some successful action
        /// </summary>
        /// <typeparam name="T">
        /// </typeparam>
        /// <param name="identity">
        /// The Identity.
        /// </param>
        /// <param name="detailedMessage">
        /// The detailed Message.
        /// </param>
        /// <param name="nestedObject">
        /// The nested Object.
        /// </param>
        /// <returns>
        /// A status populated accordingly to represent a success
        /// </returns>
        public static Status<T> Success<T>(Identity identity, string detailedMessage, T nestedObject)
        {
            return new Status<T>(identity, true, detailedMessage, nestedObject);
        }

        /// <summary>
        /// Create a status respresenting some successful action
        /// </summary>
        /// <param name="identity">
        /// The Identity.
        /// </param>
        /// <param name="detailedMessage">
        /// The detailed Message.
        /// </param>
        /// <param name="nestedStatus">
        /// The nested Status.
        /// </param>
        /// <returns>
        /// A status populated accordingly to represent a success
        /// </returns>
        public static Status Success(Identity identity, string detailedMessage, Status nestedStatus)
        {
            return new Status(identity, true, detailedMessage, nestedStatus);
        }

        /// <summary>
        /// Create a status respresenting some successful action
        /// </summary>
        /// <typeparam name="T">
        /// </typeparam>
        /// <param name="identity">
        /// The Identity.
        /// </param>
        /// <param name="detailedMessage">
        /// The detailed Message.
        /// </param>
        /// <param name="nestedStatus">
        /// The nested Status.
        /// </param>
        /// <param name="nestedObject">
        /// The nested Object.
        /// </param>
        /// <returns>
        /// A status populated accordingly to represent a success
        /// </returns>
        public static Status<T> Success<T>(
            Identity identity, string detailedMessage, Status nestedStatus, T nestedObject)
        {
            {
                return new Status<T>(identity, true, detailedMessage, nestedStatus, nestedObject);
            }
        }

        /// <summary>
        /// The to string.
        /// </summary>
        /// <returns>
        /// The to string.
        /// </returns>
        public override string ToString()
        {
            return this.Sucess ? "Success" : "Failure";
        }

        #endregion
    }
}